<template>
	<view style="overflow-x: hidden;">
		<top-heigth style="background-color: #fff;" backTitle="选择服务时间"></top-heigth>	
			
		<view class="content-box">
			<view class="public-box m-t24 back-fff">
				
				<scroll-view class="scroll-view_H" scroll-x="true" scroll-left="0">
					<view class="time-top" style="width: 1200rpx;">
						<view @click="titleChange(1)" :class="num == 1 ? 'title-font choose':'title-font'">今天</view>
						<view @click="titleChange(2)" :class="num == 2 ? 'title-font choose':'title-font'">明天</view>
						<view @click="titleChange(3)" :class="num == 3 ? 'title-font choose':'title-font'">{{getDateTime2(2)}}</view>
						<view @click="titleChange(4)" :class="num == 4 ? 'title-font choose':'title-font'">{{getDateTime2(3)}}</view>
						<view @click="titleChange(5)" :class="num == 5 ? 'title-font choose':'title-font'">{{getDateTime2(4)}}</view>
						<view @click="titleChange(6)" :class="num == 6 ? 'title-font choose':'title-font'">{{getDateTime2(5)}}</view>
						<view @click="titleChange(7)" :class="num == 7 ? 'title-font choose':'title-font'">{{getDateTime2(6)}}</view>
					</view>
				</scroll-view>
				<view class="time-box">
					<template v-for="item in list">
						<template v-if="isShowDate(item.startTime)">
							<template v-if="item.dayStatus == 1">
								<view class="time-list xiu" v-if="item.workStatus == 0">
									<view class="xiu-box">
										<view>{{item.startTime}}</view>
										<view>休息</view>
									</view>
								</view>
								<view :class="selectIndex == item.startTime ? 'time-list yue choose' : 'time-list yue'" v-if="item.workStatus == 1">
									<view class="xiu-box" @click="chooseItem(item)">
										<view>{{item.startTime}}</view>
										<view>可约</view>
									</view>
								</view>
								<view class="time-list xiu" v-if="item.workStatus == 2">
									<view class="xiu-box">
										<view>{{item.startTime}}</view>
										<view>订单</view>
									</view>
								</view>
								<view class="time-list xiu" v-if="item.workStatus == 3">
									<view class="xiu-box">
										<view>{{item.startTime}}</view>
										<view>忙</view>
									</view>
								</view>
							</template>
							<template v-if="item.dayStatus == 0">
								<view class="time-list xiu" v-if="item.workStatus == 2">
									<view class="xiu-box">
										<view>{{item.startTime}}</view>
										<view>订单</view>
									</view>
								</view>
								<view class="time-list xiu" v-else>
									<view class="xiu-box">
										<view>{{item.startTime}}</view>
										<view>休息</view>
									</view>
								</view>
							</template>
						</template>
					</template>
				</view>
			</view>
			<view class="login-but" @click="chooseDate">选择</view>
			
		</view>
	</view>
</template>

<script>
	import TopHeigth from '@/components/TopHeigth.vue'
	
	export default {
		data() {
			return {
				num:1,
				staffId: null,
				list: [],
				selectIndex: null,
				selectDate: this.getDateTime()
			}
		},
		components: { TopHeigth },
		onLoad(e) {
			this.staffId = e.staff;
			this.listBox(this.getDateTime())
		},
		methods: {
			isShowDate(itemDate){
				var itemTmp = new Date(this.selectDate + ' ' + itemDate + ':00')
				var nowTmp  = new Date()
				nowTmp.setMinutes(nowTmp.getMinutes() + 30);
				console.log(itemTmp.getTime() , nowTmp.getTime(), this.selectDate + 'T' + itemDate + ':00Z')
				if(itemTmp.getTime() > nowTmp.getTime()){
					return true
				}else{
					return false
				}
			},
			chooseItem(item){
				this.selectIndex = item.startTime
			},
			chooseDate(){
				if(!this.selectIndex){
					return uni.showToast({
						title: "请选择日期",
						icon: 'none'
					})
				}
				uni.$emit('selectDate', this.selectDate + ' ' + this.selectIndex + ':00')
				uni.navigateBack()
			},
			listBox(dateStr){
				uni.$u.http.post('/api/shop/v1_0/tAppStaffTimeConfigRCA/queryTimeStaffConfig',{
					staffId: this.staffId,
					today: dateStr
				}).then((res) => {
					this.list = res.data
				})
			},
			titleChange(n){
				this.num = n
				this.selectIndex = null
				this.selectDate = this.getDateTime(n - 1)
				this.listBox(this.selectDate)
			},
			getDateTime(day = 0){
				// 获取今天的日期
				var today = new Date();
				 
				// 创建一个新的Date对象，并设置日期为今天加一天
				var tomorrow = new Date(today);
				tomorrow.setDate(today.getDate() + day);
				 
				// 获取明天的年、月、日
				var year = tomorrow.getFullYear();
				var month = tomorrow.getMonth() + 1; // 月份从0开始，需要加1
				var day = tomorrow.getDate();
				return year + '-' + (month > 9 ? month : "0"+month) + '-' + (day > 9 ? day : "0"+day)
			},
			getDateTime2(day = 0){
				// 获取今天的日期
				var today = new Date();
				 
				// 创建一个新的Date对象，并设置日期为今天加一天
				var tomorrow = new Date(today);
				tomorrow.setDate(today.getDate() + day);
				 
				// 获取明天的年、月、日
				var year = tomorrow.getFullYear();
				var month = tomorrow.getMonth() + 1; // 月份从0开始，需要加1
				var day = tomorrow.getDate();
				return (month > 9 ? month : "0"+month) + '-' + (day > 9 ? day : "0"+day)
			}
		}
	}
</script>

<style lang="scss" scoped>
	@mixin flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: $direction;
	}
	.uv-nav-slot {
		@include flex;
		align-items: center;
		justify-content: space-between;
		border-width: 0.5px;
		border-radius: 100px;
		border-color: #dadbde;
		padding: 3px 7px;
		opacity: 0.8;
	}
	
	.time-top{
		@include flex;
		align-content: space-between;
		width: 100%;
		.title-font{
			width: 33%;
			font-size:28rpx;
			color: #333;
			text-align: center;
			padding-bottom: 16rpx;
		}
		.choose{
			font-weight: bold;
			background: url("/static/image/title-line.png") no-repeat bottom center;
		}
	}
	
	.time-box{
		@include flex;
		flex-flow: row wrap;
		padding: 20rpx 0rpx;
		.time-list{
			width:148rpx;
			height:108rpx;
			background:#F8F8F8;
			font-size:28rpx;
			color: #333;
			text-align: center;
			line-height: 112rpx;
			border-radius: 8rpx;
			margin-right: 10rpx;
			margin-bottom: 16rpx;
			border:2rpx solid #F8F8F8;
			&:nth-child(4n+4){
				margin-right: 0;
			}
		}
		.yue{
			color: #333333;
			line-height: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			
		}
		.xiu{
			color: #999;
			line-height: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			.xiu-box{
				
			}
		}
		.choose{
			color: #27A0FF;
			border:2rpx solid #27A0FF;
			background: rgba(39,160,255,0.15);
		}
	}
	.back-fff{
		background: #fff;
	}
</style>
